package com.salesys.gr.gui;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import com.salesys.gr.util.MsgBox;
import com.salesys.gr.util.FrmHelper;
import com.salesys.tony.domain.User;
import com.salesys.tony.service.UserService;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.events.TraverseEvent;

public class FrmReg {

	protected Shell FrmReg;
	private Text txtUserName;
	private Text txtNickName;
	private Text txtEmail;
	private Text txtQq;

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		FrmReg.open();
		FrmReg.layout();
		while (!FrmReg.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 * @wbp.parser.entryPoint
	 */
	protected void createContents() {
		FrmReg = new Shell(SWT.CLOSE | SWT.APPLICATION_MODAL);
		FrmReg.addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent e) {
				if (e.keyCode == 27) {
					FrmReg.close();
				}
			}
		});
		FrmReg.setSize(270, 265);
		FrmReg.setText("注册新用户");
		// 窗口居中
		FrmReg.setLocation(new FrmHelper().setLocationCenter(FrmReg));

		Label lblUserName = new Label(FrmReg, SWT.NONE);
		lblUserName.setBounds(36, 21, 70, 17);
		lblUserName.setText("用户名：　*");

		Label lblPassword = new Label(FrmReg, SWT.NONE);
		lblPassword.setBounds(36, 51, 70, 17);
		lblPassword.setText("密　码：");

		Label lblNickName = new Label(FrmReg, SWT.NONE);
		lblNickName.setBounds(36, 81, 70, 17);
		lblNickName.setText("姓　名：　*");

		Label lblEmail = new Label(FrmReg, SWT.NONE);
		lblEmail.setBounds(36, 111, 70, 17);
		lblEmail.setText("邮　箱：");

		Label lblQq = new Label(FrmReg, SWT.NONE);
		lblQq.setBounds(36, 141, 70, 17);
		lblQq.setText("QQ：");

		txtUserName = new Text(FrmReg, SWT.BORDER);
		txtUserName.setBounds(110, 20, 110, 20);

		Label lblPasswordData = new Label(FrmReg, SWT.NONE);
		lblPasswordData.setBounds(110, 50, 110, 20);
		lblPasswordData.setText("123456");

		txtNickName = new Text(FrmReg, SWT.BORDER);
		txtNickName.setBounds(110, 80, 110, 20);

		txtEmail = new Text(FrmReg, SWT.BORDER);
		txtEmail.setBounds(110, 110, 110, 20);

		txtQq = new Text(FrmReg, SWT.BORDER);
		txtQq.setBounds(110, 140, 110, 20);

		Button btnReg = new Button(FrmReg, SWT.NONE);
		btnReg.addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent arg0) {
				if (arg0.keyCode == 13) {
					reg();
				}
			}
		});
		btnReg.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				reg();
			}
		});
		btnReg.setBounds(166, 195, 88, 30);
		btnReg.setText("注　册");

		Label lblTip = new Label(FrmReg, SWT.NONE);
		lblTip.setBounds(9, 170, 248, 17);
		lblTip.setText("注：用户名建议使用英文，姓名使用真实姓名！");
	}

	public void reg() {
		User regUser = new User();
		regUser.setUsername(txtUserName.getText());
		regUser.setPassword("123456");
		if (txtNickName.getText() != "") {
			regUser.setNickname(txtNickName.getText());
		}
		if (txtEmail.getText() != "") {
			regUser.setEmail(txtEmail.getText());
		}
		if (txtQq.getText() != "") {
			regUser.setQq(txtQq.getText());
		}
		if (txtUserName.getText() == "") {
			new MsgBox().errorTip(FrmReg, "用户名不能为空");
		} else if (txtNickName.getText() == "") {
			new MsgBox().errorTip(FrmReg, "姓名不能为空");
		} else {
			try {
				if (new UserService().queryInfoFromUsername(txtUserName.getText()).getUsername() != null) {
					new MsgBox().errorTip(FrmReg, "该用户名已被注册");
				} else {
					int affectRow1 = 0;
					try {
						affectRow1 = new UserService().regUser(regUser);
					} catch (Exception e) {
						e.printStackTrace();
					}
					// int affectRow2 = 0;
					// try {
					// affectRow2 = new UserService().addMarkForSelf();
					// } catch (Exception e) {
					// e.printStackTrace();
					// }
					// int affectRow3 = 0;
					// try {
					// affectRow3 = new
					// UserService().addMarkForParent(LoginedUserHelper.getLoginedUserUsername());
					// } catch (Exception e) {
					// e.printStackTrace();
					// }
					if (affectRow1 == 1/* && affectRow2 == 1 && affectRow3 == 1 */) {
						new MsgBox().infoTip(FrmReg, "注册成功！");
						FrmReg.close();
					} else {
						new MsgBox().errorTip(FrmReg, "注册失败！");
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}